﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Example2.View
{
    /**
     * Denna klass har ansvaret att tillhandahålla utritning, ljud och effekter
     */
    class ArkanoidView
    {
        //Paddens textur
        Texture2D m_padTextureAsset;

        //Bollens textur
        Texture2D m_ballTextureAsset;


        FireParticleSystem m_spraySystem = new FireParticleSystem(new Vector2(5,5));

        /** Metod för att rita ut hela spelet
         * bakgrund
         * spelplan med brickor
         * boll
         */
        internal void Draw(Model.ArkanoidGame a_game, float a_elapsedTime, Microsoft.Xna.Framework.Graphics.SpriteBatch a_spriteBatch, Camera a_camera)
        {


            //1. Räkna ut paddens destinationsrektangel

                //hur bred är padden i vykoordinater
                int viewPadWidth = (int)(a_game.m_pad.GetWidth() * a_camera.GetScaleX());

                //paddens höjd finns inte på modellsidan så detta kan jag hårdkoda här
                int viewPadHeight = 32; 

            
                Vector2 padViewTopLeft = a_camera.toViewCoordinates(a_game.m_pad.m_centerTopPosition.X - a_game.m_pad.GetWidth() / 2, 
                                                                 a_game.m_pad.m_centerTopPosition.Y);

                Rectangle padDestinationRectangle = new Rectangle((int)padViewTopLeft.X, (int)padViewTopLeft.Y, viewPadWidth, viewPadHeight);
           
            //2. Räkna ut bollens destinationsrektangel

                //Draw ball
                Vector2 ballviewTopLeft = a_camera.toViewCoordinates(a_game.m_ball.m_centerPosition.X - a_game.m_ball.m_radius,
                                                                     a_game.m_ball.m_centerPosition.Y - a_game.m_ball.m_radius);

                int viewBallWidth = (int)(a_game.m_ball.m_radius * 2.0f * a_camera.GetScaleX());
                int viewBallHeight = (int)(a_game.m_ball.m_radius * 2.0f * a_camera.GetScaleY());

                Rectangle ballDestinationRectangle = new Rectangle((int)ballviewTopLeft.X, (int)ballviewTopLeft.Y, viewBallWidth, viewBallHeight);

            

            //Rita ut allt
            a_spriteBatch.Begin();


            //Rita ut banan
            for (int x = 0; x < Model.Level.LEVEL_WIDTH; x++)
            {
                for (int y = 0; y < Model.Level.LEVEL_HEIGHT; y++)
                {

                    if (a_game.m_level.m_tiles[x][y])
                    {
                        Vector2 tileTopLeft = a_camera.toViewCoordinates(x, y);

                        int tileWidth = (int)(Model.Level.TILE_WIDTH * a_camera.GetScaleX());
                        int tileHeight = (int)(Model.Level.TILE_HEIGHT * a_camera.GetScaleY());

                        Rectangle tileDest = new Rectangle((int)tileTopLeft.X, (int)tileTopLeft.Y, tileWidth, tileHeight);

                        a_spriteBatch.Draw(m_padTextureAsset, tileDest, Color.White);
                    }

                }
            }


            //Partikelsystemet
            m_spraySystem.UpdateAndDraw(a_elapsedTime, a_game.m_pad.m_centerTopPosition, a_game.m_pad.GetWidth() /2, a_spriteBatch, a_camera);
            
            //Padden
            a_spriteBatch.Draw(m_padTextureAsset, padDestinationRectangle, Color.White);

            //Bollen
            a_spriteBatch.Draw(m_ballTextureAsset, ballDestinationRectangle, Color.White);
            

            a_spriteBatch.End();
        }

        internal void LoadContent(Microsoft.Xna.Framework.Content.ContentManager a_content)
        {
            // Ladda in texturen pad.png från Example2Content
            m_padTextureAsset = a_content.Load<Texture2D>("pad");

            m_ballTextureAsset = a_content.Load<Texture2D>("ball");

            //Låt partikelsystemet ladda in sina texturer
            m_spraySystem.LoadContent(a_content);
        }

       
    }
}
